// pagesMer/pages/orderCoupon/detail/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
common_Js = require("../../../../component/common.js");
import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon:{},
    coupon_qr:{},
    coupon_id:0,
    coupon_qr_id:0,
    isLoading:true,
    netWork:false,
    submitState: false,
    is_can_verify: 0, // 是否可以核销
    is_expire:0,//是否已过期
    is_verify:0, //是否已核销 0 - 未核销 1 - 已核销
    isShowPopup:false,
    use_count: 1,
    remark:'',
    verify_time:'',
    verifyRecordList:[],
    isShowPopupVerifyRecord:false,
    reservationList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    console.log('onLoad options',options)
    if (options!=undefined && options.q) {
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q', q)
      options = common_Js.getQueryVariable(q);
    }

    if(options.id){
      this.data.coupon_qr_id = parseInt(options.id)
    }
    this.onHttpInit();
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/product/order/coupon/qr/detail', {
      coupon_qr_id: that.data.coupon_qr_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.data.coupon = res.data.data.coupon;
        that.data.coupon_qr = res.data.data.coupon_qr;
        that.data.is_can_verify = res.data.data.is_can_verify;
        that.data.is_expire = res.data.data.is_expire;
        that.data.coupon_id = res.data.data.coupon.id;
        that.data.is_verify = res.data.data.is_verify;
        that.data.verify_time = res.data.data.verify_time;
        that.setData({
          reservationList: res.data.data.reservationList,
          verify_time: res.data.data.verify_time,
          is_expire: res.data.data.is_expire,
          is_verify: res.data.data.is_verify,
          coupon: res.data.data.coupon,
          coupon_qr: res.data.data.coupon_qr,
          is_can_verify: res.data.data.is_can_verify,
        })
        
        that.onHttpVerifyRecord();
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onChangeUseCount(e){
    console.log('onChangeUseCount', e)
    this.data.use_count = e.detail;
  },
  inputHandle(e){
    console.log('inputHandle', e)
    this.data.remark = e.detail;
  },
  verifyRecord(){
    this.setData({
      isShowPopupVerifyRecord: !this.data.isShowPopupVerifyRecord,
    })
  },
  showRecord(){
    this.setData({
      isShowPopupVerifyRecord: !this.data.isShowPopupVerifyRecord,
    })
  },
  
  /**
   * 获取页面数据的 http请求
   */
  onHttpVerifyRecord: function () {
    var that = this;
    fetchApi.httpPost('/product/order/coupon/verify/record', {
      order_coupon_id: that.data.coupon_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.setData({
          verifyRecordList: res.data.data.verifies
        })
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goVerify(e){
    this.setData({
      isShowPopup: !this.data.isShowPopup
    })
  },
  goVerifyByReservation(e){
    var item = e.currentTarget.dataset.item;
    var that = this;
    var message = "是否要核销？";
    Dialog.confirm({
        title: '预约核销',
        message
      }).then(() => {
        that.submitOpByReservation(item.id)
      })
      .catch(() => {
        Dialog.close();
      });
  },
  submitOp(){
    if(this.data.use_count == 0){
      Toast.fail('请选择核销数量');
      return;
    }
    var that = this;
    fetchApi.httpPost('/product/order/coupon/verify', {
      use_count: that.data.use_count,
      remark: that.data.remark,
      order_coupon_id: that.data.coupon_id,
      order_coupon_qr_id: that.data.coupon_qr_id,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.setData({
          isShowPopup: false
        })
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/home/home',
          });
        }, 500)
      }else{
        Toast.fail(res.data.msg);
      }
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  submitOpByReservation(reservation_id){
    var that = this;
    fetchApi.httpPost('/product/order/coupon/verify/byReservation', {
      reservation_id: reservation_id,
      // remark: that.data.remark,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.setData({
          isShowPopup: false
        })
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/home/home',
          });
        }, 500)
      }else{
        Toast.fail(res.data.msg);
      }
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  }
})